(function(){
    var searchValue = document.getElementById('js_search_input');
    var searchForm = document.getElementById('js_search_file_form');
    var searchHitBox;
    var searchIsFocus = false;
    var searchShowHits = function(val){
        if(!searchHitBox){
            var html = '<div class="search-menu" style="display:none">' +
                            '<ul>' +
//                                '<li btn="q">' + 
//                                    '<a href="javascript:;" style="padding-left:8px;"><span>搜索站内分享资源</span></a></li>' +
                                    
                                '<li btn="search_type" t="1">'+
                                    '<a href="javascript:;"><i class="src-type st-file"></i><span>包含"<b></b>"的文件</span></a></li>' +
                                '<li btn="search_type" t="2">'+
                                    '<a href="javascript:;"><i class="src-type st-document"></i><span>包含"<b></b>"的文档</span></a></li>' +
                                '<li btn="search_type" t="3">'+
                                    '<a href="javascript:;"><i class="src-type st-photo"></i><span>包含"<b></b>"的图片</span></a></li>' +
                                '<li btn="search_type" t="4">'+
                                    '<a href="javascript:;"><i class="src-type st-music"></i><span>包含"<b></b>"的音乐</span></a></li>' +
                                '<li btn="search_type" t="9">'+
                                    '<a href="javascript:;"><i class="src-type st-video"></i><span>包含"<b></b>"的视频</span></a></li>' +
//                                '<li btn="pick_code" t="1">' + 
//                                    '<a href="javascript:;" style="padding-left:8px;"><span>搜索提取码是"<b></b>"的文件</span></a></li>' +
                            '</ul>' +
                            '<div class="search-bottom"><a href="javascript:;" btn="open_search">高级搜索</a></div>' +
                        '</div>';
            searchHitBox = $(html);
            //URL_CTL
            $(document.body).append(searchHitBox);
            searchHitBox.find("[btn]").bind("mousedown", function(){
                var btnType = $(this).attr("btn");
                switch(btnType){
                    case "pick_code":
                        searchSubmit({
                            "pick_code": "1",
                            "search_value": encodeURIComponent($.trim($(searchValue).val()))
                        })
                        break;
                    case "q":
                        searchQ();
                        break;
                    case "open_search":
                        parent.window.Main.Core.SearchFileDG.Show(searchValue.value, searchSubmit);
                        break;
                    case "search_type":
                        searchSubmit({
                            "aid": $(this).attr("t"),
                            "search_value": encodeURIComponent($.trim($(searchValue).val()))
                            
                        })
                        break;
                }
                
                return false;
            })
        }
        if(searchIsFocus && val != ""){
            searchHitBox.css("display") == "none" && searchHitBox.show();
            var oldHtml = searchHitBox.find("b:first").html();
            if(oldHtml != val){
                
                searchHitBox.find("b").html(Util.Text.Cut(val,8).replace(/</g, "&lt;").replace(/>/g, "&gt;"));
            }
        }
        else{
           searchHideHits(); 
        }
    }
    
    var focusItem = function(next){
        var className = "focus";
        var acNode = searchHitBox.find("li a." + className);
        if(next){
            if(acNode.length){
                acNode.removeClass(className);
                var nextNode = acNode.parent().next();
                if(nextNode.length){
                    nextNode.find("a").addClass(className); 
                }
            }
            else{
                searchHitBox.find("li a:first").addClass(className);
            }
        }
        else{
            if(acNode.length){
                acNode.removeClass(className);
                var prevNode = acNode.parent().prev();
                if(prevNode.length){
                    prevNode.find("a").addClass(className);
                }
            }
            else{
                searchHitBox.find("li a:last").addClass(className);
            }
        }
    }
    
    var searchQ = function(){
        
        if($.trim($(searchValue).val()) == ""){
            parent.window.Main.Core.MinMessage.Show({
                text: "请输入搜索内容",
                timeout: 2000,
                type: "war"
            })
            return false;
        }
        parent.window.Main.URL_CTL.G("sf/" + encodeURIComponent($.trim($(searchValue).val())));
    }
    
    $(searchValue).bind("focus", function(){
        searchIsFocus = true;
    }).bind("blur", function(){
        searchIsFocus = false;
    }).bind("keydown", function(e){
        if(e.keyCode == 38){
            focusItem(false);
        }
        else if(e.keyCode == 40){
            focusItem(true);
        }
    })
    
    $(searchForm).bind("submit", function(){
        var aid = -1;
        var search_pickcode = false;
        if(searchHitBox){
            var aFocus = searchHitBox.find("li a.focus");
            if(aFocus.length){
                if(aFocus.parent().attr("btn") == "pick_code"){
                    search_pickcode = true;
                }
                else if(aFocus.parent().attr("btn") == "q"){
                    searchQ();
                    return;
                }
                else{
                    aid = aFocus.parent().attr("t");
                }
            }

        }
        searchSubmit({
            "aid": aid,
            "pick_code": search_pickcode? "1" : "",
            "search_value": encodeURIComponent($.trim($(searchValue).val()))

        })
        return false;
    })

    var searchHideHits = function(){
        if(!searchIsFocus || searchValue.value ==""){
            if(searchHitBox){ 
                searchHitBox.css("display") == "none" || searchHitBox.hide(); 
            }
        }
    }
    
    window.setInterval(function(){
        var searchLabel = $(searchValue).prev();
        if(searchValue.value != ""){
            searchLabel.css("display") == "none" || searchLabel.hide();
            searchShowHits(searchValue.value);
        }
        else{
            searchLabel.css("display") == "none" && searchLabel.show();
            searchHideHits();
        }
    }, 10);
    
    if(searchValue.value != ""){
        searchValue.select();
    }
    
    var searchSubmit = function(data){
        if(!data || data["search_value"] == ""){
            parent.window.Main.Core.MinMessage.Show({
                text: "请输入搜索内容",
                timeout: 2000,
                type: "war"
            })
            return false;
        }
        if(!parent.window.Main.Core.FilePermission.Vip()){
            if(decodeURIComponent(data["search_value"]).length < 2){
                parent.window.Main.Core.MinMessage.Show({
                    text: "输入的内容不能少于2个字符",
                    timeout: 2000,
                    type: "war"
                })
                return false;
            }
        }
        data["ct"] = "file";
        data["ac"] = "search";
        var params = [];
        for(var k in data){
            params.push(k + "=" + data[k]);
        }
        var url = top.window.PAGE_PATHS["MY"]+"/index.php?r=list/index&" + params.join("&") + "&_t=" + new Date().getTime();
        window.location.href = url;
    }
    
})()